#!/bin/bash
########################################################################
# script name:  host_config.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2012/05/15    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -f               Load file (erase everything)

Ex.:
$0 -f /tmp/loadfile
STOP
}
#######################################
# Get parameters
#######################################
while getopts f: OPTION
do
	case $OPTION in
		f)
			ACTION=file
			FILE=$OPTARG
			if [ ! -f "$FILE" ]; then echo "File \"$FILE\" doesn't exist"; exit 1; fi				
			;;
		\?)
			usage
			exit 1
			;;
		*)
			usage
			exit 1
			;;
	esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
if [ -z "$ACTION" ]; then ACTION=list; fi
#######################################
# Variables and environnement
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
#######################################
# MAIN
#######################################
case $ACTION in
	edit)
		####################################################################
		# building query
		####################################################################
		for i in $HOSTS
		do	
			if [ -z "$FIRSTPASS" ]
			then 
				QUERY="SELECT * FROM hostconfig WHERE server LIKE '$i'"
				FIRSTPASS=1
			else
				QUERY="$QUERY OR server LIKE '$i'"
			fi
		done
		########################################
		# Cool loop!!
		########################################
		x=1
		while IFS=: read SERVER ENVIR SID ANF MAINT PHY HACMP SAP ORA COMMENT
		do
			Server_a[$x]="$SERVER:$ENVIR:$SID:$ANF:$MAINT:$PHY:$HACMP:$SAP:$ORA:$COMMENT"
			((x++))
		done < <(sudo -u saunix /opt/freeware/bin/mysql/mysql -u guest -pguest opmgt -Bse "$QUERY" | tr '\t' ':')
		####################################################################
		# building list of servers
		####################################################################
		for i in $HOSTS
		do
			x=1
			FOUND=no
			while [ $x -le ${#Server_a[@]} ]
			do
				SERVER=$(echo ${Server_a[$x]} | cut -d: -f1)
				if [ "$SERVER" = "$i" ]; then FOUND=yes; fi
				((x++))
			done
			if [ "$FOUND" = "no" ]; then echo "Server $i NOT in database"; fi
		done
		####################################################################
		# Editing one by one
		####################################################################
		x=1
		while IFS=: read SERVER ENVIR SID ANF MAINT PHY HACMP SAP ORA COMMENT
		do
			if [ ! -z "$SERVER" ]
			then
				echo "$SERVER:$ENVIR:$SID:$ANF:$MAINT:$PHY:$HACMP:$SAP:$ORA:$COMMENT"
			fi
			((x++))
		done < <(if [ ! -z "${Server_a[$x]}" ]; then echo ${Server_a[$x]}; fi)
		;;
	add)
		;;
	delete)
		;;
	list)
		if [ ! -z "$HOSTS" ]
		then
			for i in $HOSTS
			do	
				if [ -z "$FIRSTPASS" ]
				then 
					QUERY="SELECT * FROM hostconfig WHERE server LIKE '%$i%'"
					FIRSTPASS=1
				else
					QUERY="$QUERY OR server LIKE '%$i%'"
				fi
			done
		else
				QUERY="SELECT * FROM hostconfig"
		fi
		sudo -u saunix /opt/freeware/bin/mysql/mysql -u guest -pguest opmgt -e "$QUERY"
		;;
	file)
#INSERT INTO `opmgt`.`hostconfig` (`server`, `os`, `env`, `sid`, `anf`, `maint`, `physical`, `hacmp`, `sap`, `ora`, `comment`) VALUES ('testserver', 'aix', 'sb', 'tst', 'y', 'n', 'y', 'n', 'y', 'y', 'test server pour host_config table');
		sudo -u saunix /opt/freeware/bin/mysql/mysql -v -e "TRUNCATE opmgt.hostconfig;"
		while IFS=: read SERVER OS ENVIR SID ANF MAINT PHY HACMP SAP ORA COMMENT
		do
			QUERY="REPLACE INTO opmgt.hostconfig (server, os, env, sid, anf, maint, physical, hacmp, sap, ora, comment) VALUES ('$SERVER', '$OS', '$ENVIR', '$SID', '$ANF', '$MAINT', '$PHY', '$HACMP', '$SAP', '$ORA', '$COMMENT');"
			sudo -u saunix /opt/freeware/bin/mysql/mysql -v -e "$QUERY"
		done < <(cat $FILE | grep -v '^#' | grep .)
		;;
esac
exit
		LINE=$(sudo -u saunix /opt/freeware/bin/mysql/mysql -u guest -pguest opmgt -Bse "$QUERY")
		SERVER=$(echo $LINE | awk -F'\t' '{print $1}')
		ENVIR=$(echo $LINE | awk -F'\t' '{print $2}')
		SID=$(echo $LINE | awk -F'\t' '{print $3}')
		ANF=$(echo $LINE | awk -F'\t' '{print $4}')
		MAINT=$(echo $LINE | awk -F'\t' '{print $5}')
		PHY=$(echo $LINE | awk -F'\t' '{print $6}')
		HACMP=$(echo $LINE | awk -F'\t' '{print $7}')
		SAP=$(echo $LINE | awk -F'\t' '{print $8}')
		ORA=$(echo $LINE | awk -F'\t' '{print $9}')
		COMMENT=$(echo $LINE | awk -F'\t' '{print $10}')
